<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="layui/css/layui.css" media="all">
    <script src="layui/layui.js" charset="utf-8"></script>
<!--    <script>var $ = layui.jquery;</script>-->

</head>
<body>

<div class="layui-input-block layui-form" id="modifyClass"
     lay-filter="updateClass" style="margin-top: 30px">
    <form class="layui-form layui-form-pane" action="">
        <!--        准备一个元素,用来接受上传图片成功后回调得到的文件名(服务器生成)-->
        <input type="hidden" name="img">
        <div class="layui-form-item">
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" name="name" autocomplete="off" required lay-verify="required"
                           class="layui-input" placeholder="请输入优惠券名称">
                </div>
                <div class="layui-input-inline" style="margin-left: 264px;margin-top: -39px;">
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="upimage">上传图片</button>
                        <div class="layui-upload-list" style="height: 90px;width: 80px">
                            <img class="layui-upload-img" id="imggg" style="height: 90px;width: 80px">
                            <p id="demoText"></p>
                        </div>
                        <div style="width: 95px;">
                            <div class="layui-progress layui-progress-big" lay-showpercent="yes" lay-filter="demo">
                                <div class="layui-progress-bar" lay-percent=""></div>
                            </div>
                        </div>
                    </div>
                    <a name="list-progress"> </a>
                </div>

            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" name="desc" autocomplete="off"
                           class="layui-input" placeholder="请输入简单描述">
                </div>

            </div>


        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <input type="text" name="colletExpire" id="colletExpire" required lay-verify="required"
                       autocomplete="off" class="layui-input" placeholder="申领有效期">
            </div>
            <div class="layui-inline">
                <input type="text" name="useExpire" id="useExpire" required lay-verify="required"
                       autocomplete="off" class="layui-input" placeholder="使用有效期">
            </div>
        </div>
        <div class="layui-form-item layui-form-text" style="width: 390px">
            <label class="layui-form-label">使用规则</label>
            <textarea placeholder="请输入使用规则" required lay-verify="required" name="role"
                      class="layui-textarea"></textarea>
        </div>
        <div class="layui-form-item">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="tijiao">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </form>
</div>

<script>
    layui.use('laydate', function () {
        var laydate = layui.laydate;
        laydate.render({
            elem: '#colletExpire,#useExpire'
            , type: 'datetime'
        });
    })
</script>

<!--图片上传-->
<script>
    layui.use(['upload', 'element', 'layer'], function () {
        var $ = layui.jquery
            , upload = layui.upload
            , element = layui.element
            , layer = layui.layer;

        //常规使用 - 普通图片上传
        var uploadInst = upload.render({
            elem: '#upimage'
            // , url: 'https://httpbin.org/post' //改成您自己的上传接口
            , url: '/upload' //改成您自己的上传接口
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#imggg').attr('src', result); //图片链接（base64）
                });

                element.progress('demo', '0%'); //进度条复位
                layer.msg('上传中', {icon: 16, time: 0});
            }
            , done: function (res) {//得到后端返回的新名字  /upload/UUIDname.png
                //如果上传失败
                console.log(res);
                if (res.code > 0) {
                    return layer.msg('上传失败');
                }
                //上传成功的一些操作
                //……
                $('#demoText').html(''); //置空上传失败的状态

                //给hidden元素赋值
                $('input[name=img]').val(res.img);


            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
            //进度条
            , progress: function (n, index, e) {
                element.progress('demo', n + '%'); //可配合 layui 进度条元素使用
                if (n == 100) {
                    layer.msg('上传完毕', {icon: 1});
                }
            }
        });
    });
</script>

<script>
    layui.use('form', function () {
        var $ = layui.jquery;
        var form = layui.form;
        //监听提交
        form.on('submit(tijiao)', function (data) {
            // console.log(data);
            // return false;addData
            //开始传值请求
            $.ajax({
                url: "/cp"
                , type: "post"
                , headers: {"method": "addData"}
                , dataType: "json"
                , data: {"formData": JSON.stringify(data.field)}  //前端的所有字段
                , success: function (result) {   // result就是服务器响应之后返回的数据
                    // console.log(result.code)
                    // console.log(result.data.name);
                    // 获取返回状态为0表示添加数据成功
                    if (result.code == 0) {
                        alert(result.msg);
                        // alert("添加成功")
                    } else {
                        // alert("添加失败")
                        alert(result.msg);
                    }

                }
                , error: function (e) {
                    console.log(e)
                }
            })
            return false;
        });

    });


</script>

</body>
</html>